package com.ordersystem.demo.controller;

import com.ordersystem.demo.model.OrderDetail;
import com.ordersystem.demo.model.OrderMaster;
import com.ordersystem.demo.model.ProductInfo;
import com.ordersystem.demo.service.OrderDetailService;
import com.ordersystem.demo.service.OrderMasterService;
import com.ordersystem.demo.service.ProductInfoService;
import com.ordersystem.demo.vo.ItemProductVO;
import com.ordersystem.demo.vo.JasonVo;
import com.ordersystem.demo.vo.OrderMasterVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;


@RestController
@RequestMapping("/order")
public class OrderController {

    /**
     * 1. 参数检验 *
     * 2. 查询商品信息(调用商品服务) *
     * 3. 计算总价 *
     * 4. 扣库存(调用商品服务) *
     * 5. 订单入库
     **/
    /*
        {
        "name":"张三",
        "phone":"18868822111",
        "address":"慕课网总部",
        "openid":"ew3euwhd7sjw9diwkq",
        "items":[{
        "productId":"1423113435324",
        "productQuantity":2
        }]
        }
    * */
    @Autowired
    private OrderMasterService orderMasterService;

    @Autowired
    private ProductInfoService productInfoService;

    @Autowired
    private OrderDetailService orderDetailService;

    @PostMapping("create")
    public JasonVo orderCreate(@RequestBody OrderMasterVO orderMasterVO) {
        List<ItemProductVO> itemProductVOList = orderMasterVO.getItemList();
        BigDecimal amounts = new BigDecimal(0);
        boolean returnProductStock = true;
        for (ItemProductVO itemProductVO : itemProductVOList) {
            ProductInfo productInfo = productInfoService.findByProductIdAndProductStatus(itemProductVO.getProductId(), 0);
            if (!Optional.ofNullable(productInfo).isPresent() || productInfo.getProductStock() < itemProductVO.getProductQuantity()) {
                returnProductStock = false;
            } else {
                amounts = productInfo.getProductPrice().multiply(new BigDecimal(itemProductVO.getProductQuantity())).add(amounts);
            }
        }

        JasonVo jasonVo = new JasonVo();
        if (returnProductStock) {
            String orderId = UUID.randomUUID().toString().replace("-", "");
            OrderMaster orderMaster = new OrderMaster();
            orderMaster.setOrderId(orderId);
            orderMaster.setBuyerName(orderMasterVO.getBuyerName());
            orderMaster.setBuyerPhone(orderMasterVO.getBuyerPhone());
            orderMaster.setBuyerAddress(orderMasterVO.getBuyerAddress());
            orderMaster.setBuyerOpenid(orderMasterVO.getBuyerOpenid());
            orderMaster.setOrderAmount(amounts);
            orderMaster.setOrderStatus(0);
            orderMaster.setPayStatus(0);
            orderMaster.setCreateTime(new Date());
            orderMaster.setUpdateTime(new Date());
            orderMasterService.save(orderMaster);

            for (ItemProductVO itemProductVO : itemProductVOList) {
                ProductInfo productInfo = productInfoService.findByProductIdAndProductStatus(itemProductVO.getProductId(), 0);
                productInfo.setProductStock(productInfo.getProductStock() - itemProductVO.getProductQuantity());
                productInfoService.save(productInfo);

                OrderDetail orderDetail = new OrderDetail();
                orderDetail.setDetailId(UUID.randomUUID().toString().replace("-", ""));
                orderDetail.setOrderId(orderId);
                orderDetail.setProductId(itemProductVO.getProductId());
                orderDetail.setProductName(productInfo.getProductName());
                orderDetail.setProductPrice(productInfo.getProductPrice().multiply(new BigDecimal(itemProductVO.getProductQuantity())));
                orderDetail.setProductQuantity(itemProductVO.getProductQuantity());
                orderDetail.setProductIcon(productInfo.getProductIcon());

                orderDetailService.save(orderDetail);

            }
            jasonVo.setCode(0);
            jasonVo.setMsg("success");
            Map data = new HashMap<>();
            data.put("orderId", orderId);
            jasonVo.setData(data);
        } else {
            jasonVo.setCode(1);
            jasonVo.setMsg("failed");
        }
        return jasonVo;
    }
}
